﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Threading;
using Microsoft.Expression.Controls;
using SilverlightControlStyle.Resources;

namespace SilverlightControlStyle.Controls
{
    /// <summary>
    /// 弹出提示信息
    /// </summary>
    public class PopTipEx
    {
        private static Popup _topPopup;
        private static Callout _callout;
        private static Storyboard _storyboard;
        private static DispatcherTimer _timer;

        static PopTipEx()
        {
            if (_topPopup == null)
            {
                _topPopup = new Popup();

                _callout = new Callout
                {
                    Style = ResourceDictionaryManager.GetResource<Style>("XCalloutStyle")
                };
                _callout.MouseLeftButtonDown += OnCalloutMouseLeftButtonDown;
                _topPopup.Child = _callout;

                _storyboard = new Storyboard();
                _storyboard.Completed += OnStoryboardCompleted;
                var doubleAnnimation = new DoubleAnimation
                {
                    Duration = new Duration(TimeSpan.FromMilliseconds(1000)),
                    To = 0
                };
                _storyboard.Children.Add(doubleAnnimation);
                Storyboard.SetTarget(doubleAnnimation, _callout);
                Storyboard.SetTargetProperty(doubleAnnimation, new PropertyPath("(UIElement.Opacity)"));

                _timer = new DispatcherTimer
                {
                    Interval = TimeSpan.FromMilliseconds(2000)
                };
                _timer.Tick += OnTimerTick;
            }
        }

        /// <summary>
        /// 计时器
        /// </summary>
        private static void OnTimerTick(object sender, EventArgs e)
        {
            if (_topPopup.IsOpen)
                _storyboard.Begin();
            else
                _timer.Stop();
        }

        /// <summary>
        /// 动画完成事件
        /// </summary>
        private static void OnStoryboardCompleted(object sender, EventArgs e)
        {
            _topPopup.IsOpen = false;
        }

        /// <summary>
        /// 点击Callout
        /// </summary>
        private static void OnCalloutMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (_timer.IsEnabled)
                _timer.Stop();
            _topPopup.IsOpen = false;
        }

        /// <summary>
        /// 弹出提示信息
        /// </summary>
        /// <param name="pUi">弹出提示信息的控件</param>
        /// <param name="pMessage">提示信息</param>
        /// <param name="pHorizontalOffset">弹出位置水平偏差</param>
        /// <param name="pVerticalOffset">弹出位置垂直偏差</param>
        public static void PopTip(UIElement pUi, string pMessage, double pHorizontalOffset = 0.0, double pVerticalOffset = 45.0)
        {
            try
            {
                GeneralTransform transform = pUi.TransformToVisual(null);
                Point pt = transform.Transform(new Point(0, 0));

                _topPopup.HorizontalOffset = pt.X - pHorizontalOffset;
                _topPopup.VerticalOffset = pt.Y - pVerticalOffset;
                _topPopup.IsOpen = true;

                _storyboard.Stop();
                _callout.Content = pMessage;
                _callout.Opacity = 1.0;
                _timer.Start();
            }
            catch (ArgumentException)
            { }
        }
    }
}
